package com.zprmb.udzt;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;

import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.zprmb.udzt.view.WebViewDialog;
//import android.webkit.JavascriptInterface;
//import android.webkit.SslErrorHandler;
//import android.webkit.WebChromeClient;
//import android.webkit.WebSettings;
//import android.webkit.WebView;
//import android.webkit.WebViewClient;
//
public class HomeWebView extends WebView {
	public HomeWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
clearCache(true);
 final WebViewDialog dialog=new WebViewDialog((Activity)context);
		setWebChromeClient(chromeClient);

		// 重新设置websettings WebSettings s = wb.getSettings();
		// s.setBuiltInZoomControls(true);
		// s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		getSettings().setUseWideViewPort(true);
		
		getSettings().setLoadWithOverviewMode(true);
		getSettings().setSavePassword(true);
		getSettings().setSaveFormData(true);
		getSettings().setJavaScriptEnabled(true);
		// enable navigator.geolocation
		getSettings().setGeolocationEnabled(true);
		getSettings().setGeolocationDatabasePath(
				"/data/data/org.itri.html5webview/databases/");
		// enable Web Storage: localStorage, sessionStorage
//		getSettings().setDomStorageEnabled(true);
		requestFocus();
		getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		// setScrollBarStyle(0);
		String ua = getSettings().getUserAgentString();
		getSettings().setUserAgentString(
				ua + "; " + "rmbbox_android");
//		addJavascriptInterface(new JsInterfaces(this),
//				"AndroidWebView");
		getSettings().setBuiltInZoomControls(true);
		getSettings().setLayoutAlgorithm(
				WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		getSettings().setDomStorageEnabled(true);
		// Set cache size to 8 mb by default. should be more than enough
		getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
		// This next one is crazy. It's the DEFAULT location for your app's
		// cache
		// But it didn't work for me without this line.
		// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
		String appCachePath = context.getCacheDir()
				.getAbsolutePath();
        
		getSettings().setAllowFileAccess(true);
		getSettings().setAppCacheEnabled(true);
		getSettings().setJavaScriptEnabled(true);
		getSettings().setAppCachePath(appCachePath);
		getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		getSettings().setCacheMode(WebSettings.LOAD_NORMAL);
		getSettings().setSupportZoom(true);
		getSettings().setDefaultTextEncodingName("utf-8");
if(getX5WebViewExtension()!=null){
			
			getX5WebViewExtension().setVerticalTrackDrawable(null);
		}
		setWebViewClient(new WebViewClient(){
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
//				if (dialog!=null&dialog.isShowing() == false) {
//					dialog.show();
//				}
//				loadError = false;
				super.onPageStarted(view, url, favicon);
				
			}
			@Override
			public WebResourceResponse shouldInterceptRequest(WebView arg0,
					String arg1) {
				// TODO Auto-generated method stub
				if(arg1.contains("css")){
					
//					Log.e("样式url", arg1);
				}
				return super.shouldInterceptRequest(arg0, arg1);
			}
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
//				if (dialog!=null&dialog.isShowing() == true) {
//					dialog.dismiss();
//					
//				}
//				if (!loadError) {//当网页加载成功的时候判断是否加载成功
//	                iv_v4_nonetwork.setVisibility(View.GONE);//加载成功的话，则隐藏掉显示正在加载的视图，显示加载了网页内容的WebView
//	                
//	                v4_webview.setVisibility(View.VISIBLE);
//	               
//	            } else { //加载失败的话，初始化页面加载失败的图，然后替换正在加载的视图页面
//	            	iv_v4_nonetwork.setVisibility(View.VISIBLE);
//	            	 v4_webview.setVisibility(View.GONE);
//	            }

				super.onPageFinished(view, url);
			}
//			@Override
//			public void onReceivedSslError(WebView view,
//					SslErrorHandler handler, SslError error) {
//				// TODO Auto-generated method stub
////				super.onReceivedSslError(view, handler, error);
//				handler.proceed();
//			}
   @Override
public void onReceivedSslError(WebView arg0, SslErrorHandler arg1,
		com.tencent.smtt.export.external.interfaces.SslError arg2) {
	// TODO Auto-generated method stub
//	super.onReceivedSslError(arg0, arg1, arg2);
	   arg1.proceed();
}
		});
		
		
	}

	WebChromeClient chromeClient=new WebChromeClient(){
    	@Override
		public void onProgressChanged(WebView view, int newProgress) {
			
//    		if (newProgress == 100) {
//				dialog.dismiss();
//			} else {
//				if (dialog.isShowing() == false) {
//					dialog.show();
//				}
//				
//			}
			super.onProgressChanged(view, newProgress);
		}
    	
    };
     
	
	
}
